//
//  SearchViewController.m
//  收纳
//
//  Created by  on 2022/10/27.
//

#import "SearchViewController.h"
#import "HomeCell.h"
#import "ContainerViewController.h"

@interface SearchViewController ()
@property (strong, nonatomic) IBOutlet UIView *topView;
@property (strong, nonatomic) IBOutlet CCTextField *input;
@property (strong, nonatomic) IBOutlet UIButton *btn;
@property (strong, nonatomic) IBOutlet UILabel *nodata;
@property (nonatomic, strong) NSMutableArray <Goods *>*array;
@property (nonatomic, assign) NSInteger index;

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)initUI{
    self.index = 10000000;
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    btn.tag = 204;
    [btn setImage:[UIImage imageNamed:@"搜索"] forState:UIControlStateNormal];
    //    [btn addTarget:self action:@selector(acionforbtn:) forControlEvents:UIControlEventTouchUpInside];
    self.input.rightViewMode = UITextFieldViewModeAlways;
    self.input.rightView = btn;
    [self.tableView registerNib:[HomeCell nib] forCellReuseIdentifier:[HomeCell ID]];
    self.tableView.c_left.equalTo(self.view).coffset(20);
    self.tableView.c_right.equalTo(self.view).coffset(-20);
    self.tableView.c_top.equalTo(self.topView.c_bottom).coffset(20);
    self.tableView.c_bottom.equalTo(self.view).coffset(0);
    [self.view bringSubviewToFront:self.btn];
    [self.view bringSubviewToFront:self.nodata];
}
- (IBAction)actionForBtn:(UIButton *)sender {
    if (sender.tag == 201) {
        [self backBtnAction];
    }else if (sender.tag == 202 && self.index < self.array.count){
        ContainerViewController *vc = [ContainerViewController new];
        vc.model = self.array[self.index];
        [self push:vc :YES];
    }
}
#pragma mark - UITextFieldDelegate
- (void)textFieldDidChangeSelection:(UITextField *)textField{
    if (textField.text.length <= 0) {
        self.array = [NSMutableArray array];
        [self.tableView reloadData];
        return;
    }
    [GoodsTool search:textField.text complete:^(NSMutableArray<Goods *> * _Nonnull result) {
        self.nodata.hidden = result.count > 0;
        self.array = [NSMutableArray arrayWithArray:result];
        [self.tableView reloadData];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.array.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:[HomeCell ID]];
    cell.model = self.array[indexPath.section];
    cell.borderHexColor = self.index == indexPath.section ? @"#F6E66A" : @"#FFFFFF";
    cell.borderWidth = 1;
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.index = indexPath.section;
    [tableView reloadData];
}
@end

